package com.rain.shoppingscienceproj.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.rain.shoppingscienceproj.R;
import com.rain.shoppingscienceproj.module.Goods;
import com.rain.shoppingscienceproj.module.ShoppingcarDto;
import com.rain.shoppingscienceproj.utils.ImageUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Author: Rain
 * Create: 2018/9/3 0:07
 */
public class OrderGoodsAdapter extends RecyclerView.Adapter<OrderGoodsAdapter.MyViewHolder> {

    private Context mContext;
    private List<ShoppingcarDto> list;
    private int type = 0;
    ItemClickListener mItemClickListener;

    ItemClickEvaluateListener itemClickEvaluateListener;


    public void setItemClickEvaluateListener(ItemClickEvaluateListener itemClickEvaluateListener) {
        this.itemClickEvaluateListener = itemClickEvaluateListener;
    }

    public void setmItemClickListener(ItemClickListener mItemClickListener) {
        this.mItemClickListener = mItemClickListener;
    }

    public void setList(List<ShoppingcarDto> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    public void setType(int type) {
        this.type = type;
    }

    public OrderGoodsAdapter(Context mContext) {
        this.mContext = mContext;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.order_goods_layout, parent, false);//解决宽度不能铺满
        MyViewHolder holder = new MyViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        ShoppingcarDto goods = list.get(position);
        if (null != goods.getGoodsImage() || !"".equals(goods.getGoodsImage())) {
            ImageUtils.imageRound(mContext, goods.getGoodsImage(), holder.iv_image);
        }

        if (type == 3) {
            holder.ll_evaluate.setVisibility(View.VISIBLE);
            holder.tv_evaluate.setOnClickListener(v -> itemClickEvaluateListener.itemClickEvaluateListener(goods.getGoodsId()));
        } else {
            holder.ll_evaluate.setVisibility(View.GONE);
        }


        if (type == 1) {
            holder.ll_item.setOnClickListener(v -> {
                mItemClickListener.ItemClickListener(goods.getShopId());
            });
        }
        holder.tv_title.setText(goods.getGoodsName());
        holder.tv_money.setText(Double.valueOf(goods.getGoodsPay()) / 100 + "元");
        holder.tv_num.setText("x" + goods.getSize());
    }

    @Override
    public int getItemCount() {
        if (list == null) {
            return 0;
        } else {
            return list.size();
        }
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.iv_image)
        ImageView iv_image;

        @BindView(R.id.tv_title)
        TextView tv_title;

        @BindView(R.id.tv_money)
        TextView tv_money;

        @BindView(R.id.tv_num)
        TextView tv_num;

        @BindView(R.id.ll_item)
        LinearLayout ll_item;

        @BindView(R.id.ll_evaluate)
        LinearLayout ll_evaluate;

        @BindView(R.id.tv_evaluate)
        TextView tv_evaluate;

        public MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface ItemClickListener {
        void ItemClickListener(String shopId);
    }

    public interface ItemClickEvaluateListener {
        void itemClickEvaluateListener(String goodsId);
    }
}
